package com.googlecode.animator.controller.mediators
{
	import com.googlecode.animator.data.Notifications;
	import com.googlecode.animator.data.ProjectConstants;
	import com.googlecode.animator.data.cameras.FrontViewCameraVO;
	import com.googlecode.animator.data.cameras.LeftViewCameraVO;
	import com.googlecode.animator.data.cameras.MainViewCameraVO;
	import com.googlecode.animator.data.cameras.TopViewCameraVO;
	import com.googlecode.animator.data.viewports.ViewportVO;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class NewProjectMediator extends Mediator
	{
		/**
		 * Name prefix used to create dynamic mediator names.
		 * Names will be appended by the INSTANCE_COUNT to create a unique mediator name. 
		 */
		protected static const NAME_PREFIX:String = "NewProjectMediator_";
		
		/**
		 * Counter indicating how many instances of this class have been created. 
		 */		
		protected static var INSTANCE_COUNT:uint = 0;
		
		protected var _projectXML:XML;
		
		protected var _path:String;
		
		public function NewProjectMediator(path:String)
		{
			super(NAME_PREFIX + INSTANCE_COUNT);
			
			_path = path;
			
			// update instance count so we obtain a unique name for each Mediator instance
			INSTANCE_COUNT++;
		}
		
		override public function onRegister():void
		{
			sendNotification(Notifications.RESET_PROJECT);
		}
		
		override public function listNotificationInterests():Array
		{
			var list:Array = super.listNotificationInterests();
			
			list.push(Notifications.PROJECT_IS_RESET);
			list.push(Notifications.DEFAULT_VIEWPORTS_ADDED);
			
			return list;
		}
		
		override public function handleNotification(notification:INotification):void
		{
			super.handleNotification(notification);
			
			switch(notification.getName())
			{
				case Notifications.PROJECT_IS_RESET:
					
					handleProjectIsReset();
					
					break;
				
				case Notifications.DEFAULT_VIEWPORTS_ADDED:
					
					handleDefaultViewportsAdded();
					
					break;

			}
		}
		
		protected function handleProjectIsReset():void
		{
			
			var v1:ViewportVO = new ViewportVO();
			var v2:ViewportVO = new ViewportVO();
			var v3:ViewportVO = new ViewportVO();
			var v4:ViewportVO = new ViewportVO();
			
			var defaultCamera1:MainViewCameraVO = new MainViewCameraVO();
			var defaultCamera2:TopViewCameraVO = new TopViewCameraVO();
			var defaultCamera3:FrontViewCameraVO = new FrontViewCameraVO();
			var defaultCamera4:LeftViewCameraVO = new LeftViewCameraVO();
			
			v1.currentCamera = defaultCamera1.name;
			v2.currentCamera = defaultCamera2.name;
			v3.currentCamera = defaultCamera3.name;
			v4.currentCamera = defaultCamera4.name;
			
			v1.viewportID = ProjectConstants.VIEWPORT_1;
			v2.viewportID = ProjectConstants.VIEWPORT_2;
			v3.viewportID = ProjectConstants.VIEWPORT_3;
			v4.viewportID = ProjectConstants.VIEWPORT_4;
			
			sendNotification(Notifications.ADD_VIEWPORT, v1);
			sendNotification(Notifications.ADD_VIEWPORT, v2);
			sendNotification(Notifications.ADD_VIEWPORT, v3);
			sendNotification(Notifications.ADD_VIEWPORT, v4);
			
		}
		
		protected function handleDefaultViewportsAdded():void
		{
			sendNotification(Notifications.ADD_DEFAULT_CAMERAS, _projectXML); 
			sendNotification(Notifications.ADD_CAMERA_PROXIES, _projectXML);
			
			facade.removeMediator(getMediatorName());
		}
	}
}